﻿using System;
using System.Windows;

namespace CreateDateFile
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DateTime? dt = new DateTime();
        private string m_filePath = "";

        public MainWindow()
        {
            InitializeComponent();

            m_filePath = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);

            // DtPicker.SelectedDate = DateTime.Now;
        }

        private void ContinueBtnClick(object sender, RoutedEventArgs e)
        {
            string message = "ERRO a criar ficheiro com data de hoje";

            dt = DtPicker.SelectedDate;

            if (CreateFile())
            {
                message = "Ficheiro criado com sucesso";
            }

            MessageBox.Show(message, "Aviso");

        }

        private bool CreateFile()
        {
            try
            {
                string[] mines = new string[3];
                DateTime dt1 = (DateTime)dt;

                // First date
                int startDay = dt1.Day;
                int startMonth = dt1.Month;
                int year = dt1.Year;

                string diaStr = (dt1.Day < 10) ? ("0" + dt1.Day.ToString()) : dt1.Day.ToString();

                ValidatorOld.StartMonth m = (ValidatorOld.StartMonth)startMonth;
                string mesStr = m.ToString();

                string final = diaStr + mesStr + year.ToString().Substring(2,2);

                mines[0] = final;
                
                // Final Date
                DateTime dt2 = dt1.AddYears(1);
                startDay = dt2.Day;
                startMonth = dt2.Month;
                year = dt2.Year;

                diaStr = (dt2.Day < 10) ? ("0" + dt2.Day.ToString()) : dt2.Day.ToString();

                ValidatorOld.EndMonth m2 = (ValidatorOld.EndMonth)startMonth;
                mesStr = m2.ToString();

                final = diaStr + mesStr + year.ToString().Substring(2, 2);

                mines[1] = final;

                byte[] b = ObjectToByteArray(mines);
                System.Console.WriteLine("Wrote object: " + mines[0] + " | " + mines[1]);
                System.IO.File.WriteAllBytes(m_filePath + "\\Alimentador.rbr", b);
                return true;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("EXCEPTION: " + ex.Message + " | " + ((ex.InnerException != null) ? ex.InnerException.Message : ""));
            }

            System.Console.WriteLine("Wrote object ERROR ");
            return false;
        }

        /// <summary>
        /// Convert Object to Byte Array
        /// </summary>
        /// <param name="obj">obj</param>
        /// <returns>byte[]</returns>
        private byte[] ObjectToByteArray(Object obj)
        {
            if (obj == null)
            {
                return null;
            }

            try
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    bf.Serialize(ms, obj);
                    return ms.ToArray();
                }
            }
            catch
            {
                // Do nothing. Log when return is null on the caller's side.
            }

            return null;
        }

        private void CancelBtn_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
